<?php

namespace Admin\Controller;

class WarningController extends AdminbaseController {

    protected $warningModel;
    protected $serviceModel;
            
    function _initialize() {
        parent::_initialize();
        $this->warningModel = D("warning");
        $this->serviceModel = D("service");
    }

    function index() {

        $status = $_REQUEST['status'];
        
        if(!isNullOrEmpty($status)) {
            $map['w.status'] = $status;
            $this->assign('status', $status);
            $parameter['status'] = $status;
        }
        
        $count = $this->warningModel->alias('w')
                ->where($map)->count();
        $page = $this->page($count, 20);
        $list = $this->warningModel->alias('w')
                ->join('left join c_solar_panel sp on sp.panel_id = w.panel_id')
                ->join('left join c_area a on a.area_id = sp.area_id')
                ->join('left join c_service s on s.service_id = w.service_id')
                ->field('w.*,a.name as area_name,sp.number,s.service_name')
                ->where($map)->limit($page->firstRow . ',' . $page->listRows)->select();
       
        $this->assign('warningType', C('WARNING_TYPE'));
        $this->assign('warningStatus', C('WARNING_STATUS'));
        $this->assign("list", $list);
        $this->assign("page", $page->show('Admin', $parameter));
        $this->display();
    }

    function selectService() {
        $id = I('get.id');
        $this->assign('id', $id);
        $this->display();
    }
    
    function select_post() {
        
        if(IS_POST) {
            
            $warningId = I('post.warningId');
            $serviceId = I('post.serviceId');
            
            $warning = $this->warningModel->where(array('warning_id'=>$warningId))->find();
            
            $warning['service_id'] = $serviceId;
            $warning['status'] = C('WARNING_STATUS.WORKING');
            
            $this->warningModel->where(array('warning_id'=>$warningId))->save($warning);
            
            $service = $this->serviceModel->where(array('service_id'=>$serviceId))->find();
            $service['status'] = C('SERVICE_STATUS.BUSY');
            
            $this->serviceModel->where(array('service_id'=>$serviceId))->save($service);
            
            $this->success('指派成功', U('warning/index'));
            
        }
        
    }
    
}
